﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Drawing.Printing;
using System.Security.Permissions;
using Microsoft.Win32;


namespace WindowsFormsApplication1
{
    class Printers
    {
        public List<string> GetInstalledPrinters()
        {
            try
            {
                //set the scope of this search to the local machine
                ManagementScope scope = new ManagementScope(ManagementPath.DefaultPath);
                //connect to the machine
                scope.Connect();

                //build the SelectQuery to pull from Win32_Printer
                SelectQuery query = new SelectQuery("select * from Win32_Printer");

                ManagementClass m = new ManagementClass("Win32_Printer");

                ManagementObjectSearcher obj = new ManagementObjectSearcher(scope, query);

                List<string> printer_list = new List<string>();

                //now loop through what is found and populate our Generic list with
                //the names of all installed printers on the local machine
                using (ManagementObjectCollection printers = m.GetInstances())
                    foreach (ManagementObject printer in printers)
                        printer_list.Add(printer["Name"].ToString());

                printer_list.Sort();

                return printer_list;
            }
            catch (Exception ex)
            {
                return null;
            }

            
        }

        public void setDefaultPrinter(string printerName){
  
             ManagementScope scope = new ManagementScope(ManagementPath.DefaultPath);
             scope.Connect();

             SelectQuery oSelectQuery = new SelectQuery();
             oSelectQuery.QueryString = @"SELECT * FROM Win32_Printer WHERE Name = '" + printerName.Replace("\\", "\\\\") + "'";

             ManagementObjectSearcher oObjectSearcher = new ManagementObjectSearcher(scope, oSelectQuery);
             ManagementObjectCollection oObjectCollection = oObjectSearcher.Get();

             if (oObjectCollection.Count != 0)
             {
                 foreach (ManagementObject oItem in oObjectCollection)
                 {
                    oItem.InvokeMethod("SetDefaultPrinter", new object[] { printerName });

                 }
             }

        }
        public string getDefaultPrinter()
        {
            PrinterSettings settings = new PrinterSettings();

            foreach (string printer in PrinterSettings.InstalledPrinters)
            {
                settings.PrinterName = printer;
                if (settings.IsDefaultPrinter)
                    return printer;
            }

            return string.Empty;
        }

        public void setPageSetupFooter(string footerValue)
        {

            string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
            bool bolWritable = true;
            string strName = "footer";
            object oValue = footerValue;
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
            Console.Write(strKey);
            oKey.SetValue(strName, oValue);
            oKey.Close();

        }

        public void setPageSetupMargins(string marginLeft, string marginRight, string marginTop, string marginBottom)
        {
            if (marginLeft != "")
            {
                setPageSetupMarginLeft(marginLeft);
            }
            if (marginRight != "")
            {
                setPageSetupMarginRight(marginRight);
            }
            if (marginTop != "")
            {
                setPageSetupMarginTop(marginTop);
            }
            if (marginBottom != "")
            {
                setPageSetupMarginBottom(marginBottom);
            }

        }


        public void setPageSetupMarginLeft(string marginValue)
        {

            string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
            bool bolWritable = true;
            string strName = "margin_left";
            object oValue = marginValue;
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
            Console.Write(strKey);
            oKey.SetValue(strName, oValue);
            oKey.Close();

        }
        public void setPageSetupMarginRight(string marginValue)
        {

            string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
            bool bolWritable = true;
            string strName = "margin_right";
            object oValue = marginValue;
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
            Console.Write(strKey);
            oKey.SetValue(strName, oValue);
            oKey.Close();

        }
        public void setPageSetupMarginTop(string marginValue)
        {

            string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
            bool bolWritable = true;
            string strName = "margin_top";
            object oValue = marginValue;
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
            Console.Write(strKey);
            oKey.SetValue(strName, oValue);
            oKey.Close();

        }
        public void setPageSetupMarginBottom(string marginValue)
        {

            string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
            bool bolWritable = true;
            string strName = "margin_bottom";
            object oValue = marginValue;
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
            Console.Write(strKey);
            oKey.SetValue(strName, oValue);
            oKey.Close();

        }
    }
}
